package com.aracky.system.service.impl;

import com.aracky.common.util.ComUtil;
import com.aracky.system.model.entity.SysDept;
import com.aracky.system.model.entity.SysPost;
import com.aracky.system.model.entity.SysUser;
import com.aracky.system.model.entity.SysUserPost;
import com.aracky.system.service.*;
import com.aracky.system.wxutil.WxSyncUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author zengzhiping
 * @BelongsPackage com.aracky.system.service.impl
 * @CreateTime 2022-07-28
 * @Description 微信同步信息
 */
@Service
public class WxSyncServiceImpl implements WxSyncService {

    @Autowired
    private WxSyncUtil wxSyncUtil;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysPostService sysPostService;

    @Autowired
    private SysUserPostService sysUserPostService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncWxData() {
        Map<String, List> dataMap = wxSyncUtil.getDeptUserList();
        if (dataMap.isEmpty()) {
            return;
        }

        //用户信息
        List<SysUser> sysUserList = dataMap.get("userList");
        sysUserList = sysUserService.saveWxUser(sysUserList);

        //部门数据
        List<SysDept> sysDeptList = dataMap.get("deptList");
        sysDeptService.saveWxDept(sysDeptList);

        //岗位信息
        List<SysPost> sysPostList = dataMap.get("postList");
        sysPostList = sysPostService.saveWxPost(sysPostList);

        if (ComUtil.isNullList(sysUserList)  || ComUtil.isNullList(sysPostList)) {
            return;
        }

        //通过微信用户id构造岗位关系数据
        Map<String, Long> userMap = sysUserList.stream()
                .collect(Collectors.toMap(SysUser::getWxUserId, SysUser::getUserId,(value1,value2)->value2));

        List<SysUserPost> userPostList = new ArrayList<>(60);
        final SysUserPost[] sysUserPost = {null};
        sysPostList.stream().forEach(sysPost -> {
            sysUserPost[0] = new SysUserPost();
            sysUserPost[0].setPostId(sysPost.getPostId());
            sysUserPost[0].setUserId(userMap.get(sysPost.getWxUserId()));
            userPostList.add(sysUserPost[0]);
        });
        sysUserPostService.saveWxUserPost(userPostList);
    }

    /**
     * 岗位数据去重-去重规则 岗位名称+部门id
     *
     * @param dataMap
     * @return
     */
    public List<SysPost> distinctPost(Map<String, List> dataMap) {
        List<SysPost> sysPostList = dataMap.get("postList");
        if (sysPostList == null) {
            return sysPostList;
        }

        return sysPostList.stream()
                .collect(Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(post -> post.getDeptId() + ":" + post.getPostName()))),
                        ArrayList::new));
    }


}
